package com.lanju.dao;

import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import com.lanju.entity.YezhuCase;

@Repository
public class YezhuCaseDao {
	 private static final Log log = LogFactory.getLog(IntroduceDao.class);
	    @PersistenceContext(unitName = "financialmarketingUnit")
		private EntityManager entityManager;
	    
	    
	public void addIntroduce(YezhuCase i){
		try {
			entityManager.persist(i);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void updateIntroduce(YezhuCase i){
		try {
			entityManager.merge(i);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	public YezhuCase getIntroduceDetail(Integer id) {
		try {
			YezhuCase i=entityManager.find(YezhuCase.class, id);
			return i;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public List<YezhuCase> getAllCaseList(Map<String, Object> map){
		List<YezhuCase> list=null;
		try {
			int offset = 0;
			int limit = 0;
			if (!StringUtils.isEmpty(map.get("offset"))
					&& !StringUtils.isEmpty(map.get("limit"))) {
				offset = Integer.parseInt(map.get("offset").toString());
				limit = Integer.parseInt(map.get("limit").toString());
			}
			Query query=entityManager.createQuery("from YezhuCase ");
			query.setFirstResult(offset).setMaxResults(limit);
			 list = query.getResultList();
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		return list;
	}
	
	public long getAllCaseListCount(Map<String, Object> map){
		long count=0;
		try {
			Query query=entityManager.createQuery("select count(*) from YezhuCase ");
			count=(long) query.getSingleResult();
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		return count;
	}
}
